package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TWorkShift;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TWorkShiftDao;
import com.ruipengkj.service.TWorkShiftService;
/**
 * 交班Service层接口实现
 * @author 孙航
 *
 */
@Service
public class TWorkShiftServiceImpl implements TWorkShiftService{
	
	@Resource
	private TWorkShiftDao TWorkShiftDao;
	
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TWorkShiftDao.deleteDao(id);
	}
	
	//查询所有
	@Override
	public List<TWorkShift> findService(TWorkShift entity) throws Exception {
		String hql = "select o from TWorkShift o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TWorkShiftDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	@Override
	public TWorkShift getLastOne(TWorkShift tWorkShift)
			throws RuntimeException {
		String hql = "SELECT t  FROM TWorkShift t ORDER BY HappenTime DESC LIMIT 1";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TWorkShiftDao.findSingleEntity(hql, whereHQL.toString(), param);
	}
	
	//获取交班表最后一条记录
	public TWorkShift findLastOne(){
		String hql = "SELECT t  FROM TWorkShift t ORDER BY HappenTime DESC";
		return TWorkShiftDao.findLastOne(hql);
	}
	
	//根据登录员工获取上一条交班记录
	@Override
	public TWorkShift findLastOneByEmployee(String empID) {
		String hql = "Select t from TWorkShift t where ToEmployeeID = ? and Status = 1";
		List<Object> param = new ArrayList<Object>();
		param.add(empID);
		return TWorkShiftDao.findLastOneByEmployee(hql, param);
//		TWorkShift tws =  TWorkShiftDao.findLastOneByEmployee(hql, param);
//		if(tws!=null){
//			return tws;
//		}else{
//			return null;
//		}
	}

	
	//根据ID翻页查询
	@Override
	public PageModel<TWorkShift> getScrollService(int firstResult,
			int maxResult, TWorkShift entity) throws Exception {
		String hql = "select o from TWorkShift o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TWorkShiftDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	//根据ID翻页查询
	@Override
	public PageModel<TWorkShift> getScrollByEmployee(int firstResult,
			int maxResult, TWorkShift entity) throws Exception {
		String hql = "select o from TWorkShift o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
//		System.out.println(entity.getToEmployeeID());
		if (entity != null) {
			if(entity.getToEmployeeID()!=null && !entity.getToEmployeeID().equals("")){
				whereHQL.append("ToEmployeeID=?").append(BaseQuery.AND);
				param.add(entity.getToEmployeeID());
			}
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TWorkShiftDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//根据ID查询
	@Override
	public TWorkShift getByIdService(String id) throws Exception {
		return TWorkShiftDao.getByIdDao(id);
	}

	//修改
	@Override
	public void modifyService(TWorkShift entity) throws Exception {
		TWorkShiftDao.modifyDao(entity);
	}

	//添加或修改
	@Override
	public void saveOrUpdateService(TWorkShift entity) throws Exception {
		TWorkShiftDao.saveOrUpdateDao(entity);
	}

	//添加返回ID
	@Override
	public Serializable saveRetService(TWorkShift entity) throws Exception {
		return TWorkShiftDao.saveRetDao(entity);
	}

	//添加
	@Override
	public void saveService(TWorkShift entity) throws Exception {
		TWorkShiftDao.saveDao(entity);
	}
	
	
	
	
	

	public TWorkShiftDao getTWorkShiftDao() {
		return TWorkShiftDao;
	}

	public void setTWorkShiftDao(TWorkShiftDao tWorkShiftDao) {
		TWorkShiftDao = tWorkShiftDao;
	}

//	@Override
//	public TWorkShift getLast(TWorkShift entity) throws Exception {
//		return TWorkShiftDao.getLast(entity);
//	}
	
}
